package control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import entity.Top;
import service.TopService;
import service.TopServiceimpl;

public class Getrank  extends HttpServlet{
	TopService ts = new TopServiceimpl();
	List<Top> lst = null;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("init");
		new Thread() {
			public void run() {
				while(true) {
					try {
						sleep(3600*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						lst = ts.newToplist();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			};
		}.start();
	}
	
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(lst==null) {
			try {
				lst = ts.newToplist();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		System.out.println("ranllll");
		System.out.println(lst);
		resp.getWriter().print(JSON.toJSON(lst));
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	super.doPost(req, resp);
	}
}
